﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ChinaCustoms.Qingdao.Workflow.Permissions;
using System.Collections.ObjectModel;
using System.Configuration;
using ChinaCustoms.Framework.DeluxeWorks.Library.OGUPermission;

namespace ChinaCustoms.Qingdao.Workflow.Activities.Chooser
{
    public partial class RoleForm : Form
    {
        string applicationCodeName = string.Empty;
        public RoleForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 被选中的角色
        /// </summary>
        public string RoleIDs
        {
            get
            {
                string ret = string.Empty;

                System.Windows.Forms.CheckedListBox.CheckedItemCollection checkedItems= this.checkedListBox1.CheckedItems;
                for (int i = 0; i < checkedItems.Count; i++)
                {
                    string temp = checkedItems[i] as string;
                    {
                        if (string.IsNullOrEmpty(temp) == false)
                        {
                            int postionBegin= temp.IndexOf("[") +1;
                            int postionEnd= temp.IndexOf("]");
                            temp = temp.Substring(postionBegin, postionEnd - postionBegin);
                            ret += temp + ",";
                        }
                    }
                }

                if (ret.EndsWith(","))
                {
                    ret = ret.Remove(ret.Length-1);
                }
                return ret;
            }
            set 
            {
                //this.textBox1.Text = value;
            }
        }

        private void RoleForm_Load(object sender, EventArgs e)
        {
            this.BindRoles();
        }

        private void BindRoles()
        {
            this.applicationCodeName= ConfigurationManager.AppSettings["ApplicationCodeName"];
            if (string.IsNullOrEmpty(this.applicationCodeName) == true)
            {
                this.lableDisplay.Text = "请先在配置文件里设置应用程序代码(ApplicationCodeName)!";
            }
            else
            {
                ReadOnlyCollection<IRole> roleColl = ChinaCustomsPermissionWrapper.GetRolesInApplication(this.applicationCodeName); ;//this.PermissionClient.GetRolesInApplication(this.applicationCodeName);
                this.checkedListBox1.Items.Clear();
                if (roleColl == null || roleColl.Count==0)
                {
                    this.lableDisplay.Text = "当前应用不存在,或则应用下没有角色.";
                }
                else
                {
                    for (int i = 0; i < roleColl.Count; i++)
                    {
                        this.checkedListBox1.Items.Add(roleColl[i].Name + "[" + roleColl[i].CodeName + "]");
                    }
                }
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string selectedApplictionCodeName = string.Empty;
            selectedApplictionCodeName = ((IApplication)this.comboBox1.SelectedItem).CodeName;

            if (string.IsNullOrEmpty(selectedApplictionCodeName) == false)
            {
                ReadOnlyCollection<IRole> roleColl = ChinaCustomsPermissionWrapper.GetRolesInApplication(selectedApplictionCodeName);
                this.checkedListBox1.Items.Clear();
                for (int i = 0; i < roleColl.Count; i++)
                {
                    this.checkedListBox1.Items.Add(roleColl[i].Name + "[" + roleColl[i].CodeName + "]");
                }
            }
        }
    }
}
